/*
Navicat SQL Server Data Transfer

Source Server         : MSSQL
Source Server Version : 105000
Source Host           : .:1433
Source Database       : sales_incentive_db
Source Schema         : dbo

Target Server Type    : SQL Server
Target Server Version : 105000
File Encoding         : 65001

Date: 2015-02-15 14:26:06
*/


-- ----------------------------
-- Procedure structure for [ACM_CAL_SUM]
-- ----------------------------
DROP PROCEDURE [ACM_CAL_SUM]
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [ACM_CAL_SUM]
	@SALE_CAMPGN_ID int,
	@DELI_CAMPGN_ID int,
	@GOT_SPECIAL int,
	@HAS_SPACIAL int
AS
BEGIN
	-- exec ACM_CAL_SUM 362,364,1
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	DECLARE @SALE_CAMPGN_AMT decimal
	DECLARE @DELI_CAMPGN_AMT decimal
	DECLARE @DIFF decimal
	
	SELECT @SALE_CAMPGN_AMT=promotion_amount FROM acm_campaign WHERE id=@SALE_CAMPGN_ID
	SELECT @DELI_CAMPGN_AMT=promotion_amount FROM acm_campaign WHERE id=@DELI_CAMPGN_ID
	
	SET @DIFF = COALESCE(@DELI_CAMPGN_AMT, 0) - COALESCE(@SALE_CAMPGN_AMT, 0)
	
	IF @DIFF > 0
	BEGIN
		SELECT CONVERT(decimal,@DIFF) as SUM_RESULT
	END
	ELSE IF @DIFF < 0
	BEGIN
		IF @HAS_SPACIAL = 0
			SELECT CONVERT(decimal,@DIFF) as SUM_RESULT
		ELSE 
		BEGIN
			IF @GOT_SPECIAL = 0
				SELECT CONVERT(decimal,0) as SUM_RESULT
			ELSE
				SELECT CONVERT(decimal,@DIFF) as SUM_RESULT
		END
	END
	ELSE	--@DIFF = 0
	BEGIN
		SELECT CONVERT(decimal,0) as SUM_RESULT
	END
	
END


--IF @GOT_SPECIAL = 1
--	BEGIN
--		IF @DIFF >= 0
--			SELECT CONVERT(decimal,0) SUM_RESULT
--		ELSE
--			SELECT CONVERT(decimal,@DIFF) as SUM_RESULT
--	END
--	ELSE
--	BEGIN
--		IF @DIFF >= 0
--			SELECT CONVERT(decimal,@DIFF) as SUM_RESULT
--		ELSE
--			SELECT CONVERT(decimal,0) SUM_RESULT
--	END
GO
